<template>
	<view class="home">
		<!-- 顶部 start-->
		<view class="top-nav">
			<view class="left">
				<text>{{times}}</text>
				<text class="name">Y妍Y</text>
			</view>
			<view class="search">
				<uni-icons color="#0c34ba" type="search"></uni-icons>
				<input type="text" confirm-type="search" @confirm="doSearch" v-model="searchInput" placeholder="输入商品名称" />
			</view>
		</view>
		<!-- 顶部 end-->
		<!-- 轮播 start -->
		<view class="banner">
			<swiper :indicator-dots="true" :autoplay="true" indicator-color="#fff" :interval="3000" :duration="1000"
				circular="true" class="banner-box">
				<swiper-item v-for="(item,index) in bannerList" :key="index">
					<view class="swiper-item">
						<image :src="item.bannerImg" mode="" class="img"></image>
					</view>
				</swiper-item>

			</swiper>
		</view>
		<!-- 轮播 end -->
		<!-- 热卖推荐 start -->
		<view class="recom">
			<view class="recom-title">
				热卖推荐
			</view>
		</view>

		<view class="recom-content">
			<view class="recom-item" v-for="(item,index) in recommend" @click="toDetail(item.pid)">
				<view class="product-img">
					<image :src="item.smallImg" mode="widthFix" class="img"></image>
				</view>
				<view class="name">{{item.name}}</view>
				<view class="enname">{{item.enname}}</view>
				<view class="price">￥{{item.price}}</view>
				<view class="isHot">
					hot
				</view>
			</view>
		</view>
		<!-- 热卖推荐 end -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerList: "",
				times: "",
				recommend: "",
				searchInput:""
			}
		},
		methods: {
			// 获取轮播图
			getBanner() {
				uni.request({
					url: "http://www.kangliuyong.com:10002/banner",

					data: {
						appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA="
					},
					success: (res) => {
						if (res.statusCode == 200) {
							// let {result}=res.data 扩展运算符
							//接收 数据
							this.bannerList = res.data.result
							// console.log(this.bannerList)
						}
					}
				})
			},
			// 获取时间
			getTime() {
				let time = new Date().getHours()
				if (time > 6 && time < 12) {
					this.times = "早上好"
				} else if (time > 12 && time < 18) {
					this.times = "下午好"
				} else if (time > 18 && time < 24) {
					this.times = "晚上好"
				} else {
					this.times = "深夜好"
				}

			},
			// 获取推荐商品
			getRecom() {
				uni.request({
					url: "http://www.kangliuyong.com:10002/typeProducts",
					data: {
						appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
						key: 'isHot',
						value: 1
					},
					success: (res) => {
						this.recommend = res.data.result;
						console.log("----recommend----")
						console.log(this.recommend)
					}
				})
			}
		,
		// 跳转到详情
		toDetail(pid){
			uni.navigateTo({
				url:`../detail/detail?pid=${pid}`
			})
			
		},
		// 跳转到搜索功能
		doSearch(){
			// console.log(event.detail)
			uni.navigateTo({
				url:`../search/search?text=${this.searchInput}`
			})
			this.searchInput=''
		}
		},
		onLoad() {
			// 初始化
			this.getBanner()
			this.getTime()
			this.getRecom()
		},
	}
</script>

<style lang="less" scoped>
	.home {
		height: 188vh;
		background-color: #f7f7f7;

		.top-nav {
			display: flex;
			padding: 10rpx 30rpx;
			line-height: 60rpx;
			background-color: #fff;

			text {
				font-weight: bold;
				font-size: 24rpx;
			}

			.name {
				color: #0c34ba;
				margin: 0 30rpx;

			}

			.search {

				flex: 1;
				background-color: #f7f8fa;
				line-height: 60rpx;
				display: flex;
				border-radius: 50rpx;
				padding-left: 10rpx;

				input {
					height: 60rpx;
					line-height: 60rpx;
					font-size: 10rpx;

				}
			}
		}

		// 轮播图结构
		.banner {
			border-radius: 30rpx;
			margin: 20rpx;
			overflow: hidden;
			position: relative;

			.banner-box {
				height: 500rpx;

				.swiper-item {
					width: 100%;
					height: 100%;

					.img {
						width: 100%;
					}
				}
			}

		}


		//深度 设置 类名样式
		::v-deep.uni-swiper-dot {
			width: 50rpx;
			height: 8rpx;
			border-radius: 10rpx;
			margin-bottom: 30rpx;

		}

		// 热卖推荐
		.recom {
			height: 110rpx;
			margin: 0 30rpx;
			background-color: #fff;
			display: flex;
			align-items: center;

			.recom-title {
				width: 220rpx;
				height: 70rpx;
				line-height: 70rpx;
				border-radius: 0 35rpx 0 0;
				color: #fff;
				background-color: #0c34ba;
				text-align: center;
			}
		}

		// 热卖推荐商品
		.recom-content {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			margin: 30rpx;

			background-color: #f7f8fa;

			.recom-item {
				position: relative;
				box-sizing: border-box;
				width: 330rpx;
				padding: 20rpx;
				margin-bottom: 30rpx;
				background-color: #fff;
				border-radius: 20rpx;

				.product-img {
					width: 100%;
					border-radius: 15rpx;
					overflow: hidden;

					.img {
						width: 100%;
						display: block;
					}
				}
				.name {
					color: #646566;
					font-size: 34rpx;
					margin: 10rpx 0;
				}
				.enname {
					color: #999999;
					font-size: 30rpx;
					display: -webkit-box;
					white-space: pre-wrap;
					overflow: hidden;
					text-overflow: ellipsis;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
					margin: 10rpx 0;
				}
				.price {
					color: #0c34ba;
					font-weight: bolder;
				}
				.isHot {
					position: absolute;
					top: 20rpx;
					left: 40rpx;
					width: 70rpx;
					height: 70rpx;
					color: #fff;
					background-color: #0c34ba;
					text-align: center;
					line-height: 70rpx;
					border-radius: 0 0 10rpx 10rpx;
				}

			}
		}
	}
</style>
